<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-3-9 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		//require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'search':
				$this->searchBusiness();
				break;
			case 'info':
				$this->getBusinessInfo();
				break;
			case 'special':
				$this->getSpecialList();
				break;
			case 'search_item':
				$this->searchItem();
				break;
			case 'food_culture':
				$this->getFoodCulture();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}
	
	private function getList()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			list($lat1, $lng1, $lat2, $lng2) = get_location_range($this->request['latitude'], $this->request['longitude']);
			$this->request['latitude1'] = $lat1;
			$this->request['longitude1'] = $lng1;
			$this->request['latitude2'] = $lat2;
			$this->request['longitude2'] = $lng2;
		}
		
		$result = get_business_list($this->request);
		
		if($result['count'] < 1) $this->message('b_01', STATUS_F);
		
		$list =array();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			foreach($result['result'] as $temp)
			{
				$distance = get_distance($this->request['latitude'], $this->request['longitude'], $temp['latitude'], $temp['longitude']);
				if($distance > DEFAULT_DISTANCE)
				{
					continue;
				}
				
				$temp['distance'] = $distance;
				$temp['business_scope'] = cut_string($temp['business_scope'], 140);
				$list[] = $temp;
			}
		}
		else
		{
			$list = $result['result'];
		}
		
		//if(isset($this->request['latitude']) && isset($this->request['longitude']) && isset($this->request['field']) && $this->request['field'] == 'dist')
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			usort($result['result'], 'sort_by_dist_asc');
		}
		
		foreach($list as &$temp)
		{
			$temp['business_scope'] = cut_string($temp['business_scope'], 140);
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function searchBusiness()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			list($lat1, $lng1, $lat2, $lng2) = get_location_range($this->request['latitude'], $this->request['longitude']);
			$this->request['latitude1'] = $lat1;
			$this->request['longitude1'] = $lng1;
			$this->request['latitude2'] = $lat2;
			$this->request['longitude2'] = $lng2;
		}
		
		$result = get_business_list($this->request);
		
		if($result['count'] < 1) $this->message('b_01', STATUS_F);
		
		$list =array();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			foreach($result['result'] as $temp)
			{
				$distance = get_distance($this->request['latitude'], $this->request['longitude'], $temp['latitude'], $temp['longitude']);
				if($distance > DEFAULT_DISTANCE)
				{
					continue;
				}
				
				$temp['distance'] = $distance;
				$temp['business_scope'] = cut_string($temp['business_scope'], 140);
				$list[] = $temp;
			}
		}
		else
		{
			$list = $result['result'];
		}
		
		//if(isset($this->request['latitude']) && isset($this->request['longitude']) && isset($this->request['field']) && $this->request['field'] == 'dist')
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			usort($result['result'], 'sort_by_dist_asc');
		}
		
		foreach($list as &$temp)
		{
			$temp['business_scope'] = cut_string($temp['business_scope'], 140);
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function getBusinessInfo()
	{
		//DBFunction::getInstance()->setDebug();
		
		$bid = isset($this->request['bid']) ? $this->request['bid'] : '';
		
		if(!$bid)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_business(array('bid'=>$bid));
		
		if(!isset($result['id']) || $result['id'] < 1)
		{
			$this->message('b_01', STATUS_E);
		}
		
		$result['image'] = ($result['image'] != '') ? BASE_URL.$result['image'] : '';
		$result['image_s'] = str_replace('_o.', '_s.', $result['image']);
		$result['comment_count'] = get_comment_count(array('id'=>$bid));
		
		$this->response['status'] = STATUS_S;
		$this->response['business_info'] = $result;
	}
	
	private function getSpecialList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$result = get_business_special_list($this->request);
		
		if(!check_array($result)) $this->message('b_02', STATUS_F);
		
		foreach($result as &$temp)
		{
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($result);
		$this->response['list'] = $result;
	}
	
	private function searchItem()
	{
		//DBFunction::getInstance()->setDebug();
		
		$name = isset($this->request['name']) ? $this->request['name'] : '';
		
		$result = array();
		//if(strlen($name) > 3)
		if($name)
		{
			$result = get_travel_item_list($this->request);
		}
		
		if(!check_array($result)) $this->message('b_01', STATUS_F);
		
		$temp = array();
		$list = array();
		foreach($result as $b)
		{
			$temp['id'] = $b['id'];
			$temp['name'] = $b['name'];
			$temp['legal_person'] = $b['legal_person'];
			$temp['business_number'] = $b['number'];
			$temp['address'] = $b['address'];
			$temp['image'] = ($b['image'] != '') ? BASE_URL.$b['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
			
			$list[] = $temp;
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function getFoodCulture()
	{
		$result = get_food_culture_list($this->request);
		$list = $result['result'];
		
		if(!check_array($list)) $this->message('b_03', STATUS_F);
		
		foreach($list as &$temp)
		{
			//$temp['desc'] = cut_string($temp['desc'], 140);
			$temp['create_date'] = date('Y-m-d', $temp['create_date']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
}